package com.example.websocket;




import com.example.controller.HolySpringController;
import com.example.pojo.GuliException;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.concurrent.CopyOnWriteArraySet;

@ServerEndpoint("/websocketCamera")
@Component
public class WebSocketServerCamera {

    static Log log = LogFactory.getLog(WebSocketServerCamera.class);
    //静态变量，用来记录当前在线连接数。应该把它设计成线程安全的。
    private static int onlineCount = 0;
    //concurrent包的线程安全Set，用来存放每个客户端对应的MyWebSocket对象。若要实现服务端与单一客户端通信的话，可以使用Map来存放，其中Key可以为用户标识
    private static CopyOnWriteArraySet<WebSocketServerCamera> webSocketSet = new CopyOnWriteArraySet<WebSocketServerCamera>();
    //与某个客户端的连接会话，需要通过它来给客户端发送数据
    private Session session;
    //接受用户id
    private String userId;

    //用来判断  serialController.init();是否已经打开
    private static boolean isOpen = false;


    @OnOpen
    public void onOpen(Session session, @PathParam("serviceId") String serviceName) {
        this.session = session;
        //在线人数加1
        addOnlineCount();
        //加入到set中
        webSocketSet.add(this);
        try {

        } catch (Exception e) {
            log.error("连接异常");
            throw new GuliException(500, "数据库数据错误");
        }
    }

    @OnClose
    public void onClose() {
        //从set中删除
        webSocketSet.remove(this);
        //在线人数减一
        HolySpringController.dbId = null;
        subOnlineCount();
        log.info("有连接关闭");
    }

    @OnMessage
    public void onMessage(String message) {
        log.info("收到" + "的消息" + message);

        try {

        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @OnError
    public void onError(Session session, Throwable err) {
        log.info("连接错误");
        err.printStackTrace();
    }

    /*
    实现服务器主动推送
     */
    public static void sendMessage(String message) {
        for (WebSocketServerCamera webSocketServer : webSocketSet) {
            try {
                //给前端发送数据
                webSocketServer.session.getBasicRemote().sendText(message);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    public static synchronized void addOnlineCount() {
        WebSocketServerCamera.onlineCount++;
    }

    public static synchronized int getOnlineCount() {
        return onlineCount;
    }

    public static synchronized void subOnlineCount() {
        WebSocketServerCamera.onlineCount--;
    }

}